package com.tyj.movie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tyj.movie.entity.Users;
import com.tyj.movie.mapper.UsersMapper;
import com.tyj.movie.service.UsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tyj
 * @since 2021-06-28
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Autowired
    UsersMapper userMapper;

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    /**
     * 判断username是否存在
     * @param username
     * @return 返回user对象
     */
    @Override
    public Users judgeUsername(String username) {
        Users user = userMapper.selectOne(new QueryWrapper<Users>().eq("username", username));
        return user;
    }

    /**
     * 注册
     * @param username 账号
     * @param password 密码
     */
    @Override
    public void register(String username, String password) {
        Users user=new Users();
        user.setUsername(username);
        user.setPassword(bCryptPasswordEncoder.encode(password));
        //TODO 需要配置文件动态获取
        user.setHeadImgUrl("https://catdog-movie.oss-cn-beijing.aliyuncs.com/heapPortrait/default.png");
        user.setCreateDate(new Date());
        user.setNickname(username);
        userMapper.insert(user);
    }

    /**
     * 修改头像
     * @param users 修改的封装对象
     * @return
     */
    @Override
    public Integer editHeadImgUrl(Users users) {
        int num = userMapper.updateById(users);
        return num;
    }

    @Override
    public Integer editUsers(Long id, String nickname) {

        return userMapper.editUsers(id,nickname);
    }
}
